package com.addzero

import com.addzero.dotfiles.BizDotfiles
import com.addzero.dotfiles.EnumDefType
import com.addzero.dotfiles.EnumOsType
import com.addzero.dotfiles.EnumStatus
import com.addzero.dotfiles.Enumplatforms
import com.addzero.dotfiles.osType
import com.addzero.jimmer.toBitmask
import org.babyfish.jimmer.sql.kt.KSqlClient
import org.babyfish.jimmer.sql.kt.ast.expression.KExpression
import org.babyfish.jimmer.sql.kt.ast.expression.KNonNullExpression
import org.babyfish.jimmer.sql.kt.ast.expression.impl.SqlDSL
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestConstructor
import org.springframework.test.context.TestPropertySource

@SpringBootTest(classes = [SpringBootApp::class])
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@TestPropertySource(locations = ["classpath:application.yml"])
class EnumInsertTest(
    private val sql: KSqlClient,
) {
    @Test
    fun testEnumInsert() {
        val bizDotfiles = BizDotfiles {
            osType = listOf(EnumOsType.MAC, EnumOsType.LINUX)
            osStructure = Enumplatforms.ARM64
            defType = EnumDefType.FUNCTION
            name = "visual-studio-code"
            value = "oxxxxxxxxxxxx"
            describtion = "vscode编辑器"
            status = EnumStatus.QIYONG
            fileUrl = null
            location = "/Applications/Visual Studio Code.app"
        }
        sql.save(bizDotfiles)
        println()
    }

//    @Test
//    fun testListEnumQuery() {
//        val execute = sql.createQuery(BizDotfiles::class) {
//            val value = listOf(EnumOsType.LINUX, EnumOsType.MAC)
//            where(table.osType `enumValueIn` value)
//            select(table)
//        }.execute()
//        println()
//    }
//
//    infix fun <E : Enum<E>> KExpression<List<E>>.`enumValueIn?`(
//        enums: Collection<E>,
//    ): KNonNullExpression<Boolean>? {
//        if (enums.isEmpty()) {
//            return null
//        }
//        val bit = enums.toBitmask()
//        return sql(Boolean::class, "(%e & %v) <> 0") {
//            SqlDSL.expression(this@`enumValueIn?`)
//            SqlDSL.value(bit)
//        }
//    }
}
